<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag placeholder="请做出你的选择"  v-decorator="['zzid', validatorRules.zzid]"     dictCode='t_flower_variety_zz , name , id'>
          </j-dict-select-tag>
        </a-form-item>

        <a-form-item label="编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input disabled v-decorator="['code' , validatorRules.code]" placeholder="自动生成"></a-input>
        </a-form-item>
        <a-form-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
<!--          <a-input-number v-decorator="['packingSpecifications']" placeholder="请输入规格" style="width: 100%"/>-->
          <a-select  v-decorator="['packingSpecifications', validatorRules.packingSpecifications]" placeholder="请选择规格"  >
            <a-select-option v-for="(d,i) in psfList"  :key="d.id" :value="d.title">{{ d.title }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['flowerPoleLength', validatorRules.flowerPoleLength]" dictCode="	planting_flower_pole" placeholder="请选择单位"/>
        </a-form-item>

        <a-form-item label="等级" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['classes', validatorRules.classes]" dictCode="	planting_flower_level" placeholder="请选择等级"/>
        </a-form-item>


      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'
  import {  ajaxGetDictItems, getZZVarietyList} from '@/api/vegetableAPI'


  export default {
    name: "FlowerVarietyModal",
    components: {
      JDate,
    },
    data () {
      return {
        psfList: [], //规格
        lengthList: [], //长度信息
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          zzid: {
            rules: [
              { required: true, message: '请输入名称!'},
            ]
          },
          // name: {
          //   rules: [
          //     { required: true, message: '请输入名称!'},
          //   ]
          // },
          code: {
            rules: [
              { required: false, message: '请输入编码!'},
            ]
          },
          packingSpecifications: {
            rules: [
              { required: true, message: '请选择规格!'},
            ]
          },flowerPoleLength: {
            rules: [
              { required: true, message: '请选择单位!'},
            ]
          },classes: {
            rules: [
              { required: true, message: '请选择等级!'},
            ]
          },
        },
        url: {
          add: "/flower/flowerVariety/add",
          edit: "/flower/flowerVariety/edit",
        },
        varfList:[],
      }
    },
    created () {
      this.initpsfDict()//初始化规格
      // this.getVarietyList()
      // this.initlengthDict()//
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'zzid','name','code','state','sort','packingSpecifications','classes','flowerPoleLength','createTime','updateTime','createUser','updateUser'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
                that.close();
              }else{
                that.$message.error(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
        })
      },
      //初始化规格信息
      initpsfDict(){
        const dictCode = 'planting_packing_specifications'
        ajaxGetDictItems(dictCode, null).then((res) => {
          if (res.success) {
            this.psfList = res.result;
          }else {
            this.psfList =[]
          }
        })
      },   //
      getVarietyList(){
        getZZVarietyList({"pageSize":999}).then((res) => {
          if (res.success) {
            this.varfList = res.result.records;
          }
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'zzid','name','code','state','sort','packingSpecifications','flowerPoleLength','classes','createTime','updateTime','createUser','updateUser'))
      },



    }
  }
</script>